package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: TODO
 * @author:
 * @date: 2023/2/5 13:59
 * @version: v1.0
 */
@Service
@SuppressWarnings(value = "all")
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsDao goodsDao;
    /**
     * @description: 分页查询商品的库存信息
     * @author
     * @date 2023/2/5 16:01
     */

    @Override
    public Map<String, Object> listInventory(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {
        HashMap<String, Object> map = new HashMap<>();

        //前端是否设置默认第一页为1，如果没有设置，为0的话，需要将第一页设置为1
        //分页开始的索引【（当前页-1）*每页显示的条数】
        page = page == 0 ? 1 : page;
        int startPage=(page-1)*rows;

        List<Goods> goodslist=goodsDao.listInventory(startPage,rows,codeOrName,goodsTypeId);

    /*    TODO int cout=goodsDao.getGoodsCount(startPage,rows,codeOrName,goodsTypeId);*/
        map.put("rows", goodslist);
     /*   map.put("total")*/
        return map;
    }

    /**
     * @description: 查询所有的商品信息
     * @author 化一帆
     * @date 2023/2/10 18:20
     */
    @Override
    public Map<String, Object> list(Integer page, Integer rows, String goodsName, Integer goodsTypeId) {
        HashMap<String, Object> map = new HashMap<>();

        page=page==0?1:page;
        int startPage=(page-1)*rows;

        List<Goods> goodsList=goodsDao.getList(startPage,rows,goodsName);
        Integer count=goodsDao.count();
        map.put("rows",goodsList);
        map.put("total",count);

        return map;
    }


    /**
     * @description: 新增商品
     * @author 化一帆
     * @date 2023/2/10 19:08
     */
    @Override
    public ServiceVO save(Goods goods) {

        if(goods.getGoodsId()==null){
            //添加
            goodsDao.insert(goods);
        }else{
            //修改
            goodsDao.update(goods);
        }
        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }
}
